package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.SysDeviceType;
import com.sxhalo.fsms.domain.UserInfo;
import com.sxhalo.fsms.exception.BaseEnumError;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.ISysDeviceTypeService;
import com.sxhalo.fsms.util.ApiResult;
import com.sxhalo.fsms.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

@Controller
@RequestMapping("/api")
public class DeviceTypeController extends BaseController {

	@Autowired
	private ISysDeviceTypeService typeService;



	@ResponseBody
	@RequestMapping(value = "/device/type", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findDeviceType(Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "delete_flag=0";
		String order = " update_time desc";
		if(!isBlank(req.getParameter("typeName"))) {
			where += " and type_name like '%" + req.getParameter("typeName").trim() + "%'";
		}
		if (!isBlank(req.getParameter("typeDescription"))) {
			where += " and type_description like '%" + req.getParameter("typeDescription").trim() + "%'";
		}

		return findList(current, pageSize, where, order, typeService);
	}


	@SysControllerLog(operationName = "新增", modelName = "设备类型")
	@ResponseBody
	@RequestMapping(value = "/device/type", method = RequestMethod.POST, produces="application/json;charset=UTF-8")
	public ApiResult saveDeviceType(@RequestBody SysDeviceType record, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		record.setDeleteFlag(0);
		record.setCreateTime(nowtime);
		typeService.insertSelective(record);
		return ApiResult.retOK(req);
	}

	@SysControllerLog(operationName = "编辑", modelName = "设备类型")
	@ResponseBody
	@RequestMapping(value = "/device/type", method = RequestMethod.PUT, produces="application/json;charset=UTF-8")
	public ApiResult updateDeviceType( SysDeviceType record, Integer newTypeId, HttpServletRequest req) {
		Timestamp nowtime = new Timestamp(System.currentTimeMillis());
		record.setUpdateTime(nowtime);
		if(newTypeId.equals(record.getTypeId())){
			typeService.updateByPrimaryKeySelective(record);
		}else{
			int delTypeId=record.getTypeId();
			record.setCreateTime(nowtime);
			record.setTypeId(newTypeId);
			record.setDeleteFlag(0);
			typeService.insertSelective(record);
			typeService.deleteByPrimaryKey(delTypeId);
		}

		return ApiResult.retOK(req);
	}

	@SysControllerLog(operationName = "删除", modelName = "设备类型")
	@ResponseBody
	@RequestMapping(value = "/device/type", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult deleteDeviceType(Integer typeId, HttpServletRequest req) {
//		SysDeviceType record=new SysDeviceType();
//		record.setTypeId(typeId);
//		record.setDeleteFlag(1);
//		typeService.updateByPrimaryKeySelective(record);
		typeService.deleteByPrimaryKey(typeId);
		return ApiResult.retOK(req);
	}



	@ResponseBody
	@RequestMapping(value = "/device/type/list", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findlist(HttpServletRequest req) {
		List<SysDeviceType> list = typeService.findWhereList("delete_flag=0", "type_id");
		return ApiResult.retOK(req, list);
	}



	@ResponseBody
	@RequestMapping(value = "/device/type/validTypeId", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult validCode(Integer typeId, Integer newTypeId , HttpServletRequest req) {
		SysDeviceType deviceType = typeService.selectByPrimaryKey(newTypeId);
		if (deviceType==null) {
			return ApiResult.retOK(req);
		}
		return ApiResult.retOK(req, BaseEnumError.数据重复);
	}
}
